package com.liu.efk.zdcq.socket;


import com.liu.common.utils.JacksonUtil;
import com.liu.efk.zdcq.pojo.CommadSocketPojo;
import com.liu.efk.zdcq.pojo.Task;
import com.liu.efk.zdcq.sevice.CollectService;
import com.liu.efk.zdcq.util.TaskIDGen;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;


import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by shi860715@126.com
 * Date 2021/5/12 10:00
 * Description
 */
@ServerEndpoint("/webSocket/{username}")
@Slf4j
@Component
public class WebSocket {

    private static int onlineCount = 0;

    private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>();
    private static Map<String, Task> tasks = new ConcurrentHashMap<String, Task>();

    private Session session;

    private String username;





    @OnOpen

    public void onOpen(@PathParam("username") String username, Session session) throws IOException {


        this.username = username;

        this.session = session;


        addOnlineCount();

        clients.put(username, this);

        System.out.println("已连接");
        System.out.println("username" + username);

    }


    @OnClose

    public void onClose() throws IOException {

        clients.remove(username);

        subOnlineCount();
        System.out.println("连接关闭");
    }


    @OnMessage
    public void onMessage(String message,Session session) throws  Exception{
      String taskId =  TaskIDGen.getTaskId();
        tasks.put(taskId,new Task(username,"2",message));

        Map map = new HashMap();
        map.put("taskid",taskId);
        map.put("commad",message);
        session.getBasicRemote().sendText(JacksonUtil.toJSon(map));

    }


    @OnError
    public void onError(Session session, Throwable error) {

        error.printStackTrace();

    }


    public void sendMessageTo(String message, String To) throws IOException {


        for (WebSocket item : clients.values()) {

                if (item.username.equals(To))

                    item.session.getBasicRemote().sendText(message);



        }

    }


    public void sendMessageAll(String message) throws IOException {

        for (WebSocket item : clients.values()) {

            item.session.getBasicRemote().sendText(message);

        }

    }


    public static synchronized int getOnlineCount() {

        return onlineCount;

    }


    public static synchronized void addOnlineCount() {

        WebSocket.onlineCount++;

    }


    public static synchronized void subOnlineCount() {

        WebSocket.onlineCount--;

    }


    public static synchronized Map<String, WebSocket> getClients() {

        return clients;

    }


    public static synchronized Map<String, Task> getTask() {

        return tasks;

    }

}
